﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Serna
{
    public static partial class Serializers    
    {

        /// <summary>
        /// This extension Serializes object to Serna-String
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="ss"></param>
        /// <returns></returns>
        public static string SerializeToSernaString(this object obj, SerializationSettings ss)
        {
            //[SerializationSettings ss = new SerializationSettings(){  DateTimeFormat =  SerializationSettings.eDateTimeFormat.EuropeanVisual}]

            /*
             {}\' are special symbols must be followed by \ to be as a text 
             {}' must not be followed by \ to be as an object element
             */
            try
            {

                //SerializationSettings ss = new SerializationSettings();
                // ss.StringSerialization_SkipUnnecessarySmbols = true;
                //ss.DateTimeFormat = SerializationSettings.eDateTimeFormat.EuropeanVisual;

                InternalSerializer intSer = new InternalSerializer();
                return intSer.Serialize(obj, ss);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// This extension Serializes object to Serna-String
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string SerializeToSernaString(this object obj)
        {
            SerializationSettings ss = new SerializationSettings();
            // ss.StringSerialization_SkipUnnecessarySmbols = true;
            ss.DateTimeFormat = SerializationSettings.eDateTimeFormat.EuropeanVisual;

            return SerializeToSernaString(obj, ss);
        }//eom









        /// <summary>
        /// Deserializes object from Serna-String to object T
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="str"></param>
        /// <returns></returns>
        public static T DeserializeFromSernaString<T>(this string str, SerializationSettings ss)
        {
            InternalDeserializer des = new InternalDeserializer();

            return (T)des.Deserialize(typeof(T), str, ss);
        }


        /// <summary>
        /// Deserializes object from Serna-String to object T
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="str"></param>
        /// <returns></returns>
        public static T DeserializeFromSernaString<T>(this string str)
        {
            SerializationSettings ss = new SerializationSettings();
            ss.DateTimeFormat = SerializationSettings.eDateTimeFormat.EuropeanVisual;

            return DeserializeFromSernaString<T>(str, ss);

        }













        public static string SerializeToJsonString(this object obj)
        {           
            try
            {
                InternalJSONSerializer intSer = new InternalJSONSerializer();
                return intSer.Serialize(obj);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        /// <summary>
        /// Can read
        /// <para>{"p1":"113028906","p2": 12.485,"p3":{"ip1":34,"ip2":"dfs"},"p4":[{"ip1":33,"ip2":"fff"},"sdfs","dsfd"]}</para>
        /// <para>var re = t.DeserializeFromJsonStringToDictionary();</para>
        /// <para>var x1 = re["p4"].DeserializeFromJsonStringToDictionary();</para>
        /// <para>var x2 = x1.First().Key.DeserializeFromJsonStringToDictionary();</para>
        /// <para>when first is [-always array; {-new object</para>
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static Dictionary<string, string> DeserializeFromJsonStringToDictionary(this string str)
        {
            Serna.Serializers.InternalJsonDeserializerToDictionary id = new Serializers.InternalJsonDeserializerToDictionary();
            return id.Deserialize(str);

        }



        ///// <summary>
        ///// Deserializes object from Serna-String to object T
        ///// </summary>
        ///// <typeparam name="T"></typeparam>
        ///// <param name="str"></param>
        ///// <returns></returns>
        //public static T DeserializeFromJsonString<T>(this string str)
        //{
        //    InternalJsonDeserializer des = new InternalJsonDeserializer();
        //    des.s

        //    return (T)des.Deserialize(typeof(T), str, ss);
        //}


    }
}
